package com.xd.core.business.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xd.common.constant.SysConstant;
import com.xd.common.exception.XkdException;
import com.xd.common.util.SecurityUtil;
import com.xd.common.util.XkdUtil;
import com.xd.core.bill.dto.OverduePolicyRefundQuery;
import com.xd.core.bill.vo.OverduePolicyRefundVo;
import com.xd.core.business.dto.EndorSementDto;
import com.xd.core.business.dto.GuaranteeQueryDto;
import com.xd.core.business.dto.GuaranteeReviewDto;
import com.xd.core.business.entity.GuaranteeSlip;
import com.xd.core.business.mapper.GuaranteeSlipMapper;
import com.xd.core.business.service.IGuaranteeSlipService;
import com.xd.core.business.vo.GuaranteePageVo;
import com.xd.core.business.vo.ReviewVo;
import com.xd.core.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 保单表 服务实现类
 * </p>
 *
 * @author ZX
 * @since 2025-06-20
 */
@Service
public class GuaranteeSlipServiceImpl extends ServiceImpl<GuaranteeSlipMapper, GuaranteeSlip> implements IGuaranteeSlipService {

    @Autowired
    private GuaranteeSlipMapper guaranteeSlipMapper;
    @Autowired
    private ISysUserService iSysUserService;

    /***
     * @description 保单列表分页查询
     * @author Zch
     * @date 11:45 2025/6/23 星期一
     * @return com.github.pagehelper.Page<com.xd.core.business.vo.GuaranteePageVo>
     **/
    @Override
    public Page<GuaranteePageVo> getGuaranteeList(GuaranteeQueryDto dto) {
        String companyType = SecurityUtil.getLoginUser().getUser().getCompanyType();
        Integer companyId = null;
        List<Integer> userIds = null;
        if (companyType.equals(SysConstant.USER_COMPANY_TYPE_ONE) || companyType.equals(SysConstant.USER_COMPANY_TYPE_FOUR)) {
            companyId = dto.getCompanyId();
        } else if (companyType.equals(SysConstant.USER_COMPANY_TYPE_TOW)) {
            companyId = SecurityUtil.getLoginCompanyId();
            userIds = iSysUserService.getUserIdsByGroup();
        } else if (companyType.equals(SysConstant.USER_COMPANY_TYPE_THREE)) {//代理
            return null;
        }
        //开始分页
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        return guaranteeSlipMapper.getGuaranteeList(dto, companyId, userIds);
    }

    /***
     * @description 修改复核状态
     * @author Zch
     * @date 17:05 2025/6/20 星期五
     * @return void
     **/
    @Override
    public void guaranteeReview(GuaranteeReviewDto dto) {
        XkdUtil.checkNonNullProperties(dto, "guaranteeId", "reviewStatus");
        guaranteeSlipMapper.guaranteeReview(dto);
    }

    /***
     * @description 复核数据回显
     * @author Zch
     * @date 11:44 2025/6/23 星期一
     * @return com.xd.core.business.vo.ReviewVo
     **/
    @Override
    public ReviewVo getReview(Integer guaranteeId) {
        if (StrUtil.isEmpty(guaranteeId.toString()))
            throw new XkdException("参数错误");
        return guaranteeSlipMapper.getReview(guaranteeId);
    }

    /***
     * @description 批单上传
     * @author Zch
     * @date 11:44 2025/6/23 星期一
     * @return void
     **/
    @Override
    public void uploadEndorSement(EndorSementDto dto) {
        XkdUtil.checkNonNullProperties(dto, "guaranteeId", "endorsementUrl");
        LambdaQueryWrapper<GuaranteeSlip> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GuaranteeSlip::getId, dto.getGuaranteeId());
        GuaranteeSlip guaranteeSlip = this.getOne(wrapper);
        if (ObjectUtil.isEmpty(guaranteeSlip) || (!guaranteeSlip.getStatus().equals("0")))
            throw new XkdException("保单不存在或当前状态不允许上传批单");
        guaranteeSlip.setEndorsementUrl(ObjectUtil.isEmpty(guaranteeSlip.getEndorsementUrl()) ? dto.getEndorsementUrl() : guaranteeSlip.getEndorsementUrl() + "," + dto.getEndorsementUrl());
        this.updateById(guaranteeSlip);
    }

    /***
     * @description 上传保单
     * @author Zch
     * @date 15:07 2025/6/23 星期一
     * @return void
     **/
    @Override
    public void uploadSingle(EndorSementDto dto) {
        XkdUtil.checkNonNullProperties(dto, "guaranteeId", "guaranteeSlipUrl");
        String operationUser = SecurityUtil.getLoginUser().getUser().getUserName();
        LambdaQueryWrapper<GuaranteeSlip> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GuaranteeSlip::getId, dto.getGuaranteeId());
        GuaranteeSlip guaranteeSlip = this.getOne(wrapper);
        if (ObjectUtil.isEmpty(guaranteeSlip) || (!guaranteeSlip.getStatus().equals("0")))
            throw new XkdException("保单不存在或当前状态不允许上传保单");
        guaranteeSlip.setGuaranteeSlipUrl(dto.getGuaranteeSlipUrl());
        guaranteeSlip.setUploadUser(operationUser);
        guaranteeSlip.setUploadDate(new Date());
        this.updateById(guaranteeSlip);
    }

    /***
     * @description 逾期退保列表分页查询
     * @author Zch
     * @date 10:09 2025/7/7 星期一
     * @return com.github.pagehelper.Page<com.xd.core.bill.vo.OverduePolicyRefundVo>
     **/
    @Override
    public Page<OverduePolicyRefundVo> getOverduePolicyRefundList(OverduePolicyRefundQuery query) {
        String companyType = SecurityUtil.getLoginUser().getUser().getCompanyType();
        Integer companyId = null;
        List<Integer> userIds = null;
        if (companyType.equals(SysConstant.USER_COMPANY_TYPE_ONE) || companyType.equals(SysConstant.USER_COMPANY_TYPE_FOUR)) {
            companyId = query.getCompanyId();
        } else if (companyType.equals(SysConstant.USER_COMPANY_TYPE_TOW)) {
            companyId = SecurityUtil.getLoginCompanyId();
            userIds = iSysUserService.getUserIdsByGroup();
        } else if (companyType.equals(SysConstant.USER_COMPANY_TYPE_THREE)) {//代理
            return null;
        }
        //开始分页
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        return guaranteeSlipMapper.getOverduePolicyRefundList(query, companyId, userIds);
    }
}
